package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{
	@Autowired
	private CourierService courierService;

	private Courier courier = new Courier();
	@Override
	public Courier getModel() {
		// TODO Auto-generated method stub
		return courier;
	}
	private int page;
	private int rows;
	
	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="curier_Add",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String curier_Add(){
		courierService.save(courier);
		return SUCCESS;
	}
	@Action(value="courierFindAll",results={@Result(name="success",type="json")})
	public String courierFindAll(){
		//System.out.println();
		System.out.println(page);
		System.out.println(rows);
		Pageable pageAble = new PageRequest(page - 1, rows);
		Specification<Courier> sa = new Specification<Courier>() {
		
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<>();
				if(StringUtils.isNotBlank(courier.getCourierNum())){
					Predicate p = cb.equal(root.get("courierNum").as(String.class),courier.getCourierNum());
				//	System.out.println(courier.getCourierNum());
					list.add(p);
				}
				if(StringUtils.isNotBlank(courier.getCompany())){
					Predicate p = cb.like(root.get("company").as(String.class),"%" +courier.getCompany() + "%");
					//System.out.println(courier.getCompany());
					list.add(p);
				}
				if(StringUtils.isNotBlank(courier.getType())){
					Predicate p = cb.equal(root.get("type").as(String.class),courier.getType());
					//System.out.println(courier.getType());
					list.add(p);
				}
				Join<Courier,Standard> j = root.join("standard",JoinType.INNER);
				if(courier.getStandard() != null && StringUtils.isNotBlank(courier.getStandard().getName())){
					Predicate p = cb.equal(j.get("name").as(String.class),courier.getStandard().getName());
					//System.out.println(courier.getStandard().getName());
					list.add(p);
				}
				Predicate predicates = cb.and(list.toArray(new Predicate[0]));
				return predicates;
			}
			
			
		};
		Page<Courier> pp = courierService.courierFindAll(sa,pageAble);
		Map<String,Object> map = new HashMap<>();
		map.put("total", pp.getTotalElements());
		map.put("rows", pp.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	@Action(value="courier_delBatch",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String delBatch(){
		String[] arr = ids.split(",");
		courierService.delBatch(arr);
		return SUCCESS;
	}
	@Action(value="courierFindAll1",results={@Result(name="success",type="json")})
	public String courierFindAll1(){
		
		List<Courier> cc = courierService.courierFindAll1();
		System.out.println(cc);
		ActionContext.getContext().getValueStack().push(cc);
		return SUCCESS;
	}
}
